package com.rdxer.core.security;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.Resource;


public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    protected UserDetailsService userDetailsService;
    @Resource
    protected PasswordEncoder bCryptPasswordEncoder;
    @Autowired
    protected JWTConfiguration jwtConfiguration;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors()
                .and()
                .csrf()
                .disable();
        http.rememberMe().disable();
        // 禁用缓存
        http.headers().cacheControl().disable();

        http
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
//                .sessionAuthenticationFailureHandler(new JWTAuthenticationFailureHandler(result))
        ;

        http.authorizeRequests()
                // 任何  OPTIONS 放行
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                // 任何  OPTIONS 放行 end

                .antMatchers("/test/*").permitAll()

                .antMatchers("/init").permitAll()
                .antMatchers("/initwf").permitAll()

                 .antMatchers("/client/**").permitAll()


                // swagger start
                .antMatchers("/swagger-ui.html").permitAll()
                .antMatchers("/docs.html").permitAll()
                .antMatchers("/swagger-resources/**").permitAll()
                .antMatchers("/images/**").permitAll()
                .antMatchers("/webjars/**").permitAll()
                .antMatchers("/v2/api-docs").permitAll()
                .antMatchers("/configuration/ui").permitAll()
                .antMatchers("/configuration/security").permitAll()
                // swagger end


                .anyRequest().authenticated()
//        new JWTAuthenticationFailureHandler(result)
        // 任何 请求都需要进行 身份验证
        ;

        http
//                .addFilter(new JWTLoginFilter(authenticationManager(),jwtConfiguration))
                .addFilter(new JWTAuthenticationFilter(authenticationManager(), userDetailsService,jwtConfiguration));


    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .userDetailsService(userDetailsService)
                .passwordEncoder(bCryptPasswordEncoder)
        ;
    }
}
